package com.lab.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.lab.dao.CourseDao;
import com.lab.dao.impl.CourseDaoImpl;
import com.lab.po.Course;
import com.lab.po.PageBean;
import com.lab.po.Student;


@WebServlet("/courseAjaxServlet")
public class CourseAjaxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public CourseAjaxServlet() {
        super();
       
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int currentPage=1;
		if(request.getParameter("currentPage")!=null){
			currentPage=Integer.parseInt(request.getParameter("currentPage"));
		}
		//ajax请求
		CourseDao cd=new CourseDaoImpl();
		PageBean pagebean=new PageBean();
		pagebean.setCurrentPage(currentPage);
		System.out.println(currentPage);
		String cou_name=request.getParameter("cou_name");

		Map map=new HashMap();
		if(cou_name!=null && !cou_name.equals("")){
			map.put("cou_name", "%"+cou_name+"%");
		}
		pagebean.setCountRecords(cd.countCouByCondition(map));
		map.put("key1", (currentPage-1)*pagebean.getCountPage());
		map.put("key2",pagebean.getCountPage());
		List<Course> courseList=cd.queryCourseByCondition(map);
		JSONArray jsonArray=JSONArray.fromObject(courseList);
		response.getWriter().write(jsonArray.toString());
	}

}
